package cq.com.cn.utils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;


public class JDBCUtils {

	 //增加、删除、修改
	 public static void operater(String sql,Object...obj) {
		 Connection con=null;
		 PreparedStatement ps=null;
		try {
			   con = C3p0Utils.getConnection();
			   ps = con.prepareStatement(sql);			 
			  if(obj!=null){
				  for(int i=0;i<obj.length;i++){
					  ps.setObject(i+1, obj[i]);
				  }
			  }
			  ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			C3p0Utils.release(con,ps,null);
		}
	 }
	 //查询
	 public static <T> List<T> findList(Class<T> c,String sql,Object...obj) {
			Connection con=null;
			PreparedStatement ps=null;
			ResultSet rs=null;
			List<T> tList=new ArrayList<>();
			try {
				con = C3p0Utils.getConnection();
				ps = con.prepareStatement(sql);
				if(obj!=null){
					 for(int i=0;i<obj.length;i++){
						 ps.setObject(i+1, obj[i]);
					 }
				}
				rs = ps.executeQuery();
				
				//获取此 ResultSet 对象的列的编号、类型和属性
				ResultSetMetaData meta = rs.getMetaData();
				//获取列数
				int column=meta.getColumnCount();
				
				while(rs.next()){
					T t = c.newInstance();
					for(int i=1;i<=column;i++) {
						//获取列名
						String columnName = meta.getColumnName(i);
						//获取属性对象
						Field f = c.getDeclaredField(columnName);

						//根据列名获取值
						Object value = rs.getObject(columnName);
						
						f.setAccessible(true);
						
						if(value!=null) {
							if(value instanceof java.sql.Date) {
								java.sql.Date	ds=(Date) value;
								f.set(t, new java.util.Date(ds.getTime()));
							 }else {
								f.set(t, value);
							 }
						}
						f.setAccessible(false);
					}
					tList.add(t);
				}				
			}catch (SQLException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			}finally {
				C3p0Utils.release(con,ps,null);
			}
			tList.stream().forEach(System.out::println);
			
		 return tList;
	 }

	//查询
	public static <T> T findOne(Class<T> c,String sql,Object...obj) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		T t=null;
		try {
			con = C3p0Utils.getConnection();
			ps = con.prepareStatement(sql);
			if(obj!=null){
				for(int i=0;i<obj.length;i++){
					ps.setObject(i+1, obj[i]);
				}
			}
			rs = ps.executeQuery();

			//获取此 ResultSet 对象的列的编号、类型和属性
			ResultSetMetaData meta = rs.getMetaData();
			//获取列数
			int column=meta.getColumnCount();

			while(rs.next()){
				t = c.newInstance();
				for(int i=1;i<=column;i++) {
					//获取列名
					String columnName = meta.getColumnName(i);
					//获取属性对象
					Field f = c.getDeclaredField(columnName);

					//根据列名获取值
					Object value = rs.getObject(columnName);

					f.setAccessible(true);

					if(value!=null) {
						if(value instanceof java.sql.Date) {
							java.sql.Date	ds=(Date) value;
							f.set(t, new java.util.Date(ds.getTime()));
						}else {
							f.set(t, value);
						}
					}
					f.setAccessible(false);
				}
			}
		}catch (SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}finally {
			C3p0Utils.release(con,ps,null);
		}

		return t;
	}
	
	
	public static java.sql.Date convert(java.util.Date ds){
		java.sql.Date date=new java.sql.Date(ds.getTime());
		return date;
	}

}
